package com.yiya.ms.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.Sample;
import com.yiya.bean.SysUser;
import com.yiya.model.SampleCplex;
import com.yiya.model.SampleCplexModel;
import com.yiya.service.SampleService;
import com.yiya.utils.HtmlUtil;
import com.yiya.utils.SessionUtils;

@Controller
@RequestMapping("/sample")
public class SampleAction extends BaseAction {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(SampleAction.class);

	@Autowired
	private SampleService sampleService;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}

	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("sample/sample", context);
	}

	@RequestMapping("/dataList")
	public void datalist(SampleCplexModel model,
			HttpServletResponse response) throws Exception {
		List<SampleCplex> list = sampleService
				.queryByList(model);
		int count = sampleService.queryByCount(model);
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("rows", list);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}

	@RequestMapping("/save")
	public void save(Sample bean, HttpServletRequest request,
			HttpServletResponse response) {
		Date nowTime = new Date();
		SysUser user = SessionUtils.getUser(request);
		if (bean.getId() == null) {
			bean.setCreateTime(nowTime);
			bean.setCreateBy(user.getId());
			sampleService.add(bean);
		} else {
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			sampleService.Update(bean);
		}
		sendSuccessMessage(response, "保存成功");
	}

	@RequestMapping("/getId")
	public void getId(Integer id, HttpServletResponse response) {
		Map<String, Object> context = getRootMap();
		Sample sample = sampleService.queryById(id);
		if (sample == null) {
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", sample);
		HtmlUtil.writerJson(response, context);
	}

	@RequestMapping("/delete")
	public void delete(Integer[] id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		SysUser user = SessionUtils.getUser(request);
		sampleService.delete(id, user);
		sendSuccessMessage(response, "删除成功");
	}
}
